package com.company;

/**
 * Java 值传递
 * https://leetcode.cn/leetbook/read/deep-learning-java-from-bug/7lyoo7/
 * @date : 2023-03-28 下午5:20
 */

public class P051_PassByValue {

    public static void main(String[] args) {
        // Create a person named Bob and he has no money.
        Person person = new Person();
        person.name = "Bob";
        person.money = 0;
        // Check the person, if he has no money, set it as null
        check(person);
        // If the person turned to null, print he has no money, otherwise print he's rich
        if (person == null) {
            System.out.println(person.name + " has no money. money: " + person.money);
        } else {
            // 值传递
            System.out.println(person.name + " is rich.  money: " + person.money);
        }
    }

    private static void check(Person person) {
        if (person.money <= 0) {
            // 值传递
            person.name = "Apple";
            person.money = 999999;
            // 引用传递
            person = null;
        }
    }

    private static class Person {
        private String name;
        private int money;
    }
}
